﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace VisualFaml
{
    public class ConsoleBox : TextBox
    {
        private StringBuilder sb = new StringBuilder();

        public ConsoleBox()
        {
            this.ReadOnly = true;
            this.Multiline = true;
        }

        public void Put(string format, params object[] args)
        {
            Put(String.Format(format, args));
        }

        public void Put(string message)
        {
            System.Diagnostics.Debug.WriteLine(message);

            if (sb == null)
                throw new ApplicationException("ConsoleBox is not initialized (call InitOutput)");

            sb.Append(message);
            sb.Append('\n');
            string[] lines = sb.ToString().Split('\n');
            this.Lines = lines;
        }

        public void Put(Exception exc)
        {
            do
            {
                System.Diagnostics.Debug.WriteLine(exc.Message);
                sb.Append(/*"Exception: " + */exc.Message + "\n");
                sb.Append("\n\n\nStack Trace: " + exc.StackTrace + "\n\n\n\n");
                exc = exc.InnerException;
            } while (exc != null);
            string[] lines = sb.ToString().Split('\n');
            this.Lines = lines;
        }

        internal void ClearAll()
        {
            sb = new StringBuilder();
        }
    }
}
